package com.wufei.tastefor.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wufei.tastefor.BaseApp;
import com.wufei.tastefor.R;
import com.wufei.tastefor.fragment.ActivitiesFragment;
import com.wufei.tastefor.fragment.DynamicFragment;
import com.wufei.tastefor.fragment.HomeFragment_;
import com.wufei.tastefor.fragment.MyFragment;
import com.wufei.tastefor.fragment.NewsFragment_;
import com.wufei.tastefor.utils.AppConfig;
import com.wufei.tastefor.utils.AppManager;
import com.wufei.tastefor.utils.FragmentTabUtils;
import com.wufei.tastefor.utils.IsFirstUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;

@EActivity(R.layout.activity_main)
public class MainActivity extends BaseActivity {

    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    @ViewById
    public RadioGroup mainFootRgs;
    private long time;
    private Platform platform;

    @AfterViews
    public void initView() {
        getSupportActionBar().hide();
        fragments.add(new HomeFragment_());
        fragments.add(new NewsFragment_());
        fragments.add(new DynamicFragment());
        fragments.add(new ActivitiesFragment());
        fragments.add(new MyFragment());
        new FragmentTabUtils(getSupportFragmentManager(), this, fragments, R.id.main_fragment_container, mainFootRgs, new FragmentTabUtils.OnRgsExtraCheckedChangedListener() {

            @Override
            public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index, int preIndex) {
                switch (index) {
                    case 0:
                        break;
                    case 1:
                        if (IsFirstUtils.isLogin(BaseApp.getInstance()) == false) {
                            ((RadioButton) radioGroup.getChildAt(preIndex)).setChecked(true);
                            loginPopUpWindow();
                        }
                        break;
                    case 2:
                        if (IsFirstUtils.isLogin(BaseApp.getInstance()) == false) {
                            ((RadioButton) radioGroup.getChildAt(preIndex)).setChecked(true);
                            loginPopUpWindow();
                        }
                        break;
                    case 3:
                        break;
                    case 4:
                        if (IsFirstUtils.isLogin(BaseApp.getInstance()) == false) {
                            ((RadioButton) radioGroup.getChildAt(preIndex)).setChecked(true);
                            loginPopUpWindow();
                        }
                        break;
                }
            }
        });

    }

    private void loginPopUpWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.login_popup_window_layout, null);
        ImageView cancel = (ImageView) view.findViewById(R.id.close_pop);
        final RadioGroup rgs = (RadioGroup) view.findViewById(R.id.pop_login_rgs);
        ((RadioButton) rgs.getChildAt(0)).setChecked(true);
        final RelativeLayout login = (RelativeLayout) view.findViewById(R.id.pop_login_rl);
        final RelativeLayout regist = (RelativeLayout) view.findViewById(R.id.pop_regist_rl);
        login.setVisibility(View.VISIBLE);
        regist.setVisibility(View.GONE);
        TextView qq = (TextView) view.findViewById(R.id.qq_login);
        TextView sina = (TextView) view.findViewById(R.id.sina_weibo_login);

        final PopupWindow popupWindowLogin = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, false);
        //设置popwindow的背景颜色
        popupWindowLogin.setBackgroundDrawable(new ColorDrawable(0x55000000));
        //点击popwindow以外的空间,pop消失
        popupWindowLogin.setOutsideTouchable(true);
        popupWindowLogin.setFocusable(true);
        popupWindowLogin.showAtLocation(view, Gravity.CENTER, 0, 0);

        rgs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (((RadioButton) rgs.getChildAt(0)).isChecked()) {
                    login.setVisibility(View.VISIBLE);
                    regist.setVisibility(View.GONE);
                } else if (((RadioButton) rgs.getChildAt(1)).isChecked()) {
                    login.setVisibility(View.GONE);
                    regist.setVisibility(View.VISIBLE);
                }
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindowLogin.dismiss();
            }
        });

        qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                platform = ShareSDK.getPlatform(BaseApp.getInstance(), QQ.NAME);
                platform.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        SharedPreferences sp = BaseApp.getInstance().getSharedPreferences(AppConfig.APP_IS_LOGIN, Context.MODE_PRIVATE);
                        PlatformDb db = platform.getDb();
                        sp.edit().putBoolean(AppConfig.APP_IS_LOGIN_KEY, true).commit();
                        sp.edit().putString(AppConfig.APP_USERNAME, db.getUserName()).commit();
                        sp.edit().putString(AppConfig.APP_USER_ICON, db.getUserIcon()).commit();
                        sp.edit().putString(AppConfig.APP_USER_SEX, db.getUserGender()).commit();
                        popupWindowLogin.dismiss();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        BaseApp.getInstance().showToast("登录失败");
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {

                    }
                });
                platform.authorize();
                //移除授权
                //platform.removeAccount();
            }
        });
        sina.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                platform = ShareSDK.getPlatform(BaseApp.getInstance(), SinaWeibo.NAME);
                platform.SSOSetting(false);  //设置false表示使用SSO授权方式
                platform.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        SharedPreferences sp = BaseApp.getInstance().getSharedPreferences(AppConfig.APP_IS_LOGIN, Context.MODE_PRIVATE);
                        PlatformDb db = platform.getDb();
                        sp.edit().putBoolean(AppConfig.APP_IS_LOGIN_KEY, true).commit();
                        sp.edit().putString(AppConfig.APP_USERNAME, db.getUserName()).commit();
                        sp.edit().putString(AppConfig.APP_USER_ICON, db.getUserIcon()).commit();
                        sp.edit().putString(AppConfig.APP_USER_SEX, db.getUserGender()).commit();
                        popupWindowLogin.dismiss();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        BaseApp.getInstance().showToast("登录失败");
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {

                    }
                });
                platform.authorize();
                //移除授权
                //platform.removeAccount();
            }
        });
    }

    @Override
    public void onBackPressed() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - time > 1000) {
            time = currentTime;
            BaseApp.getInstance().showToast("再按一次退出程序");
        } else {
            AppManager.getAppManager().AppExit(BaseApp.getInstance());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApp.getInstance().unegisterNetworkStateReceiver();
    }
}
